package com.barbeque.mall.service;

import com.barbeque.mall.bean.category.*;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.mapper.MarketCategoryMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 *
 * @author Tezel
 * @date 2022/07/17
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    MarketCategoryMapper categoryMapper;


    @Override
    public CommonData<MarketCategoryL1VO> queryL1() {

        MarketCategoryExample categoryExample = new MarketCategoryExample();

        MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(0);


        categoryExample.setDistinct(true);

        List<MarketCategory> marketCategories = categoryMapper.selectByExample(categoryExample);

        List<MarketCategoryL1VO> list = new ArrayList<>();

        for (MarketCategory marketCategory : marketCategories) {
            MarketCategoryL1VO marketCategoryL1VO = new MarketCategoryL1VO(marketCategory.getId(),marketCategory.getName());
            list.add(marketCategoryL1VO);
        }
        PageInfo<MarketCategoryL1VO> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketCategory> queryList() {
        MarketCategoryExample categoryExample= new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(0);



        //第一级类目
        List<MarketCategory> marketCategorieL1s = categoryMapper.selectByExample(categoryExample);

        for (MarketCategory marketCategorieL1 : marketCategorieL1s) {

            MarketCategoryExample categoryExample1 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria1 = categoryExample1.createCriteria();
            criteria1.andPidEqualTo(marketCategorieL1.getId());

            //二级类目
            List<MarketCategory> marketCategorieL2s = categoryMapper.selectByExample(categoryExample1);
            marketCategorieL1.setChildren(marketCategorieL2s);

        }


        PageInfo<MarketCategory> pageInfo = new PageInfo<>(marketCategorieL1s);

        return CommonData.data(pageInfo);
    }

    @Override
    public MarketCategoryCreateVO create(MarketCategoryCreateBO marketCategoryCreateBO) {

        MarketCategory marketCategory = new MarketCategory();

        Date date = new Date(System.currentTimeMillis());


        BeanUtils.copyProperties(marketCategoryCreateBO, marketCategory);
        marketCategory.setAddTime(date);
        marketCategory.setUpdateTime(date);
        marketCategory.setDeleted(false);

        //TODO sort_order沒有用到

        //创建一个类目
        categoryMapper.insertSelective(marketCategory);


        //返回信息
        MarketCategory category = categoryMapper.selectByPrimaryKey(marketCategory.getId());

        //封装进VO
        MarketCategoryCreateVO marketCategoryCreateVO = new MarketCategoryCreateVO();
        BeanUtils.copyProperties(category, marketCategoryCreateVO);

        return marketCategoryCreateVO;

    }

    @Override
    public void update(MarketCategory marketCategory) {


        MarketCategoryExample categoryExample = new MarketCategoryExample();

        MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();

        Date date = new Date(System.currentTimeMillis());

        if(marketCategory.getPid() == 0  ){
            //一级变二级 类别选0,L2
            //二级变一级 pid 0 L1 children = null

            List<MarketCategory> children = marketCategory.getChildren();
            //一级目录变二级，它的子目录的pid也得
            if(children != null) {
                for (MarketCategory child : children) {
                    child.setPid(0);
                    child.setUpdateTime(date);
                    MarketCategoryExample categoryExample1 = new MarketCategoryExample();
                    MarketCategoryExample.Criteria criteria1 = categoryExample1.createCriteria();
                    criteria1.andIdEqualTo(child.getId());
                    categoryMapper.updateByExampleSelective(child, categoryExample1);

                }
            }

        }else if (marketCategory.getPid().intValue() == marketCategory.getId().intValue()){

            //一级目录底下有二级目录，一级目录变二级pid  并且父类目选自己,导致隐藏
            marketCategory.setPid(0);


            List<MarketCategory> children = marketCategory.getChildren();
            //一级目录变二级，它的子目录的pid也得
            for (MarketCategory child : children) {
                child.setPid(0);
                child.setUpdateTime(date);
                MarketCategoryExample categoryExample2 = new MarketCategoryExample();
                MarketCategoryExample.Criteria criteria2 = categoryExample2.createCriteria();
                criteria2.andIdEqualTo(child.getId());
                categoryMapper.updateByExampleSelective(child,categoryExample2);

            }

        }
        else {
            //一级类目变二级类目，且选择其他类目
            List<MarketCategory> children = marketCategory.getChildren();
            //一级目录变二级，它的子目录的pid也得
            for (MarketCategory child : children) {
                child.setPid(0);
                child.setUpdateTime(date);
                MarketCategoryExample categoryExample3 = new MarketCategoryExample();
                MarketCategoryExample.Criteria criteria3 = categoryExample3.createCriteria();
                criteria3.andIdEqualTo(child.getId());
                categoryMapper.updateByExampleSelective(child, categoryExample3);
            }
        }

        marketCategory.setUpdateTime(date);
        criteria.andIdEqualTo(marketCategory.getId());

        categoryMapper.updateByExampleSelective(marketCategory,categoryExample);


    }

    /**
     * 删除商品类目
     * @author Tezel
     * @date 2022/07/17
     */

    @Override
    public void delete(MarketCategory marketCategory) {

        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
        Integer id = marketCategory.getId();
        criteria.andIdEqualTo(id);

        List<MarketCategory> children = marketCategory.getChildren();

        if(children != null){
            for (MarketCategory child : children) {
                Integer childId = child.getId();
                MarketCategoryExample categoryExample1 = new MarketCategoryExample();
                MarketCategoryExample.Criteria criteria1 = categoryExample1.createCriteria();
                criteria1.andIdEqualTo(childId);
                categoryMapper.deleteByExample(categoryExample1);

            }
        }

        categoryMapper.deleteByExample(categoryExample);


    }
}
